using System;
using System.Collections;
using System.Collections.Generic;
using Server.Items;
using Server.Mobiles;
using Server.Gumps;


namespace Server.Items
{
	public class BidAccountBook : Item
	{
		public static readonly int MaxEntry = 60;

		private int m_BookSerial;
		private Mobile m_Holder;

		public int BookSerial
		{
			get{ return m_BookSerial; }
			set{ m_BookSerial = value; InvalidateProperties(); }
		}

		public Mobile Holder
		{
			get{ return m_Holder; }
			set{ m_Holder = value; InvalidateProperties(); }
		}


		[Constructable]
		public BidAccountBook() : this( int.MinValue )
		{
		}

		[Constructable]
		public BidAccountBook( int serial ) : base( 0x2254 )
		{
			Name = "an account book";
			Hue = 1260;
			Weight = 1.0;

			m_Holder = null;
			m_BookSerial = serial;
		}

		public BidAccountBook( Serial serial ) : base( serial )
		{
		}


		public bool IsFull( Mobile from )
		{
			if ( SaveStone.PlayerBids != null && SaveStone.PlayerBids.ContainsKey( from ) )
			{
				List<BidRecord> list = (List<BidRecord>)SaveStone.PlayerBids[from];

				if ( list != null && list.Count > MaxEntry )
					return true;
			}

			return false;
		}

		public override void OnDoubleClick( Mobile from )
		{
			if ( m_Holder == null )
			{
				m_Holder = from;
				from.SendMessage( "Now you are the holder of the Bid account book." );
				return;
			}

			Maintenance.TrimBidRecords( from );

			if ( SaveStone.PlayerBids != null && SaveStone.PlayerBids.ContainsKey( from ) )
			{
				List<BidRecord> list = (List<BidRecord>)SaveStone.PlayerBids[from];

				if ( list != null && list.Count > 0 )
				{
					from.CloseGump( typeof( BidAccountGump ) );
					from.SendGump( new BidAccountGump( from, 0 ) );
				}
				else
					from.SendMessage( "The book is empty." );
			}
			else
				from.SendMessage( "There's no bid record.");

			base.OnDoubleClick( from );
		}

		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );
			writer.Write( (int)0 ); //version

			writer.Write( (int) m_BookSerial );
			writer.Write( (Mobile) m_Holder );
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );
			int version = reader.ReadInt();

			m_BookSerial = reader.ReadInt();
			m_Holder = reader.ReadMobile();
		}
	}
}
